package mypodsync.service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import mypodsync.GeneralConfig;
import mypodsync.device.DeviceListener;
import mypodsync.device.DeviceManager;

public class ServiceManager implements DeviceListener {
    
    private Logger log = Logger.getLogger(ServiceManager.class.getName());
    private List<Service> services;
    private DeviceManager deviceManager;
    private GeneralConfig generalConfig;
    
    public ServiceManager(GeneralConfig generalConfig) {
        this.generalConfig = generalConfig;
        services = new ArrayList<Service>();
        deviceManager = new DeviceManager(this);
        BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/services.properties")));
        String line = null;
        try {
            while ((line = br.readLine()) != null) {
                Object obj = Class.forName(line).newInstance();
                Service serv = (Service)obj;
                serv.setServiceManager(this);
                services.add(serv);
                deviceManager.addDevice(serv);
            }
        } catch (Exception ex) {
            log.log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
    
    public void activate(Service service) {
        log.fine(service.toString());
        service.execute();
    }
    
    public void deactivate(Service service) {
        log.fine(service.toString());
    }

    public List<Service> getServices() {
        return services;
    }

    public GeneralConfig getGeneralConfig() {
        return generalConfig;
    }

}

